package com.tsinjie.metadata.java.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by LiuXunming on 2015-11-7.
 * Java项目
 */
public class JavaProject implements Serializable {
    /**
     * 工程名称
     */
    private String projectName;
    /**
     * 根包
     */
    private String basePackage;
    /**
     * 作者
     */
    private String author;
    /**
     * 日期
     */
    private Date createDate;
    /**
     * 登录验证码
     * 0 不启用
     * 1 启用
     */
    private Integer useCaptcha;
    /**
     * 工程描述
     */
    private String remark;
    /**
     * 数据库连接
     */
    private String url;
    /**
     * 用户名
     */
    private String username;
    /**
     * 密码
     */
    private String password;
    /**
     * 数据库方言
     */
    private String dialect;
    /**
     * 数据库名称
     */
    private String databaseProductName;
    /**
     * 驱动名称
     */
    private String driverName;
    /**
     * java类信息列表
     */
    private List<JavaClass> javaClasses;
    /**
     * 登录类
     */
    private JavaClass loginClass;

    private Map<String, JavaClass> javaClassMap = new HashMap<String, JavaClass>();

    public JavaProject() {
        this.useCaptcha = 0;
    }

    public String getProjectName() {
        return projectName == null ? getDatabaseName() : projectName;
    }

    public void setProjectName(String projectName) {
        this.projectName = projectName;
    }

    public String getBasePackage() {
        return basePackage;
    }

    public void setBasePackage(String basePackage) {
        this.basePackage = basePackage;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public Date getCreateDate() {
        return createDate;
    }

    public void setCreateDate(Date createDate) {
        this.createDate = createDate;
    }

    public Integer getUseCaptcha() {
        return useCaptcha;
    }

    public void setUseCaptcha(Integer useCaptcha) {
        this.useCaptcha = useCaptcha;
    }

    public String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }

    public List<JavaClass> getJavaClasses() {
        return javaClasses;
    }

    public void setJavaClasses(List<JavaClass> javaClasses) {
        this.javaClasses = javaClasses;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getDialect() {
        return dialect;
    }

    public void setDialect(String dialect) {
        this.dialect = dialect;
    }

    public String getDatabaseProductName() {
        return databaseProductName;
    }

    public void setDatabaseProductName(String databaseProductName) {
        this.databaseProductName = databaseProductName;
    }

    public String getDriverName() {
        return driverName;
    }

    public void setDriverName(String driverName) {
        this.driverName = driverName;
    }

    public JavaClass getLoginClass() {
        if (loginClass == null && javaClasses != null) {
            for (JavaClass jc : javaClasses) {
                if (jc.getLoginClass() == JavaClass.LOGIN_CLASS) {
                    loginClass = jc;
                    break;
                }
            }
        }
        return loginClass;
    }

    public void setLoginClass(JavaClass loginClass) {
        this.loginClass = loginClass;
    }

    public Map<String, JavaClass> getJavaClassMap() {
        return javaClassMap;
    }

    public void setJavaClassMap(Map<String, JavaClass> javaClassMap) {
        this.javaClassMap = javaClassMap;
    }

    /**
     * 添加单个javaClass到javaClasses列表
     *
     * @param javaClass
     */
    public void addJavaClass(JavaClass javaClass) {
        if (this.javaClasses == null) {
            this.javaClasses = new ArrayList<JavaClass>();
        }
        this.javaClasses.add(javaClass);
    }

    /**
     * 根据表名称获取JavaClass
     *
     * @param tableName
     * @return JavaClass
     */
    public JavaClass getJavaClass(String tableName) {
        JavaClass javaClass = javaClassMap.get(tableName);
        if (this.javaClasses != null && javaClass == null) {
            for (JavaClass jc : this.javaClasses) {
                javaClassMap.put(jc.getTableName(), jc);
                if (jc.getTableName().equals(tableName)) {
                    javaClass = jc;
                }
            }
        }
        return javaClass;
    }

    /**
     * 根据数据库url获取数据库名称
     *
     * @return
     */

    public String getDatabaseName() {
        if (url == null) {
            return null;
        }
        String dn = null;
        int index = url.lastIndexOf("/");
        if (index != -1) {
            dn = url.substring(index + 1);
            index = dn.indexOf("?");
            if (index != -1) {
                dn = dn.substring(0, index);
            }
        }
        return dn;
    }

    @Override
    public String toString() {
        return "JavaProject{" +
                "projectName='" + projectName + '\'' +
                ", basePackage='" + basePackage + '\'' +
                ", author='" + author + '\'' +
                ", createDate=" + createDate +
                ", useCaptcha=" + useCaptcha +
                ", remark='" + remark + '\'' +
                ", url='" + url + '\'' +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", dialect='" + dialect + '\'' +
                ", databaseProductName='" + databaseProductName + '\'' +
                ", driverName='" + driverName + '\'' +
                ", javaClasses=" + javaClasses +
                ", javaClassMap=" + javaClassMap +
                '}';
    }
}
